<template>
  <div>
    <common-head title="添加联系人"></common-head>
    <div class="wrap">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
      />
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { areaList } from '@vant/area-data'
import { addLinkman } from '@api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      areaList
    }
  },
  components: {
    CommonHead
  },
  methods: {
    onSave (linkman) {
      // 点击保存联系人
      const obj = {}
      for (const key in linkman) {
        switch (key) {
          case 'addressDetail':
            obj.address = linkman[key]
            break
          case 'areaCode':
            obj.cityId = linkman[key]
            break
          case 'name':
            obj.linkMan = linkman[key]
            break
          case 'tel':
            obj.mobile = linkman[key]
            break
          case 'postalCode':
            obj.provinceId = linkman[key]
            break
          default:
            break
        }
      }
      obj.isDefault = linkman.isDefault
      console.log(obj)
      addLinkman({
        ...obj,
        token: localStorage.getItem('token')
      }).then(res => {
        Toast({
          type: 'success',
          message: '添加联系人成功',
          onClose: () => {
            this.$router.go(0)
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 50px;
  background-color: #fff;
}
</style>
